#include "lvgl_app/lvgl_app.h"

/*枚举
界面ID
    PM_ID_MENU

界面参数
    无
*/

/* 建议采样50*50的图标 */
/* https://www.iconfont.cn/collections/detail?spm=a313x.7781069.0.da5a778a4&cid=748 */
LV_IMG_DECLARE(IconFile);
LV_IMG_DECLARE(IconSetting);
LV_IMG_DECLARE(lv_img_app);
static const lv_img_dsc_t* btn_img_list[6] =
{
    &IconSetting,
    &IconFile,
    &lv_img_app,
    &lv_img_app,
    &lv_img_app,
    &lv_img_app,
};
/* 语言切换 start*/
static const char* btn_lab_str_list_en[] =
{ "Setting","File","File","file","photo","setting" };
static const char* btn_lab_str_list_zh[] =
{ "系统设置","文件","应用","file","photo","setting" };
static char** btn_lab_str_list = (char**)btn_lab_str_list_zh;
/* 语言切换 end*/

/**
 * @brief 处理按钮事件的回调函数
 * @param event
*/
static void button_event_cb(lv_event_t* event)
{
    lv_obj_t* current_btn = lv_event_get_current_target(event);
    lv_obj_t* container = event->user_data;
    uint32_t current_btn_index = lv_obj_get_index(current_btn);
    uint32_t mid_btn_index = (lv_obj_get_child_cnt(container) - 1) / 2;

    if (event->code == LV_EVENT_FOCUSED)
    {
        if (current_btn_index > mid_btn_index)
        {
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index - 1), LV_ANIM_OFF);
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_ON);
            lv_obj_move_to_index(lv_obj_get_child(container, 0), -1);
        }
        else if (current_btn_index < mid_btn_index)
        {
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index + 1), LV_ANIM_OFF);
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_ON);
            lv_obj_move_to_index(lv_obj_get_child(container, -1), 0);
        }
    }
}

static void button_event_cb2(lv_event_t* event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if (code == LV_EVENT_SHORT_CLICKED)
    {
        /* 通过传入的user data得到当前按键的index */
        uint32_t index = ((size_t)(event->user_data) - (size_t)(&btn_lab_str_list[0])) / sizeof(size_t);
        //        printf("index -> %d \r\n", index);
        switch (index)
        {
        case 0:
        {
            lv_pm_control.open(PM_ID_SETTING_MENU, &options_default);
        }break;
        default:
            lv_pm_control.open(PM_ID_MAIN, &options_default);
            break;
        }
    }
}

static void page_onLoad(void* pm_page)
{
    lv_obj_t* page = lv_pm_get_pm_page(pm_page)->page;
    lv_obj_set_style_bg_opa(page, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(page, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_SCROLLED);

    lv_obj_t* container = lv_obj_create(page);
    /* 使用弹性布局 */
    lv_obj_set_size(container, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_flex_flow(container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_scroll_snap_x(container, LV_SCROLL_SNAP_CENTER);
    lv_obj_set_style_bg_opa(container, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(container, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(container, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_column(container, 60, LV_PART_MAIN);   //图标之间的间隙
    lv_obj_set_style_pad_top(container, 30, LV_PART_MAIN);      //图标与顶部的间隙
    lv_obj_center(container);

    for (int i = 0; i < 3; i++)
    {
        lv_obj_t* btn_class = lv_btn_create(container);
        lv_obj_set_size(btn_class, 70, 70);
        lv_obj_set_style_bg_opa(btn_class, 0, LV_PART_MAIN);
        lv_obj_set_style_outline_opa(btn_class, 0, LV_STATE_FOCUS_KEY);

        lv_obj_add_event_cb(btn_class, button_event_cb, LV_EVENT_ALL, container);
        lv_obj_add_event_cb(btn_class, button_event_cb2, LV_EVENT_SHORT_CLICKED, &btn_lab_str_list[i]);

        lv_obj_t* btn_limg = lv_img_create(btn_class);
        lv_img_set_src(btn_limg, btn_img_list[i]);
        lv_obj_align(btn_limg, LV_ALIGN_CENTER, 0, -8);
        lv_obj_set_size(btn_limg, 50, 50);

        lv_obj_t* btn_lab = lv_label_create(btn_class);
        lv_obj_align(btn_lab, LV_ALIGN_BOTTOM_MID, 0, 8);
        lv_label_set_text(btn_lab, btn_lab_str_list[i]);
    }

    uint32_t mid_btn_index = (lv_obj_get_child_cnt(container) - 1) / 2;
    for (uint32_t i = 0; i < mid_btn_index; i++)
    {
        lv_obj_move_to_index(lv_obj_get_child(container, -1), 0);
    }
    /*当按钮数为偶数时，确保按钮居中*/
    lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_OFF);
    lv_pm_create.focus_obj(pm_page, lv_obj_get_child(container, mid_btn_index));
}

static void page_unLoad(void* pm_page)
{
    lv_obj_t* page = lv_pm_get_pm_page(pm_page)->page;
}

void page_create_page_menu(lv_pm_id_t id)
{
    lv_pm_page_t* page = lv_pm_create.create(id, page_onLoad, page_unLoad);
    lv_pm_create.status_bar(page, true);
    lv_pm_create.back_bar(page, false);
}

